<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="考试名称" prop="className">
        <el-input
          v-model="queryParams.examName"
          placeholder="请输入考试名称"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试日期" prop="status">
        <el-date-picker
          v-model="queryParams.examDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="状态" prop="classDept">
        <el-select v-model="queryParams.status" clearable size="small" style="width: 180px">
          <el-option value="0" label="停用"></el-option>
          <el-option value="1" label="启用"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-circle-check"
          size="mini"
          @click="operationStatus(1)"
          :disabled="multiple"
        >启用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-circle-close"
          size="mini"
          @click="operationStatus(0)"
          :disabled="multiple"
        >停用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleDeleteBatch()"
          :disabled="multiple"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="el-icon-download" size="mini" @click="HandleImportTemp">导出考试模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-upload2" size="mini" @click="handleImport">导入考试信息</el-button>
      </el-col> -->
      
    </el-row>

    <el-table v-loading="loading" :data="bclassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="学期" align="center" prop="termRemarks" :show-overflow-tooltip="true" />
      <el-table-column label="考试课程" align="center" prop="dictLabel" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="考试名称" align="center" prop="examName" :show-overflow-tooltip="true" />
      <el-table-column label="考试开始时间" align="center" prop="examBeginTime" width="160"></el-table-column>
      <el-table-column label="考试结束时间" align="center" prop="examEndTime" width="160"></el-table-column>
      <el-table-column label="监考老师" align="center" prop="invigilationTeacher" width="160"></el-table-column>
      <el-table-column label="考试教室" align="center" prop="levelName" width="160"></el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <label v-if="scope.row.status==0">停用</label>
          <label v-if="scope.row.status==1">启用</label>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            circle
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          />
          <el-button
            size="mini"
            circle
            type="danger"
            icon="el-icon-delete"
            :style="{display:scope.row.isGradeEnter==true?'none':''}"
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="考试名称" prop="examName">
          <el-input v-model="form.examName" placeholder="请输入班级名称" />
        </el-form-item>
        <!-- <el-form-item label="考试专业" prop="classSubject">
            <el-select v-model="form.classSubject" clearable filterable style="width:100%">
              <el-option v-for="item in subjectList" :label="item.subjectName" placeholder="班级专业" :value="item.id" :key="item.id"></el-option>
            </el-select>
        </el-form-item>-->
        <el-form-item label="考试课程" prop="curriculumType">
          <el-select v-model="form.curriculumType" clearable filterable style="width:100%">
            <el-option
              v-for="item in courseTypeOptions"
              :label="item.dictLabel"
              placeholder="选择考试课程"
              :value="item.dictValue"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试班级" prop="classId">
          <el-select v-model="form.classId" clearable filterable style="width:100%">
            <el-option
              v-for="item in classOptions"
              :label="item.className"
              placeholder="选择考试班级"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试开始时间" prop="examBeginTime">
          <el-date-picker
            v-model="form.examBeginTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="考试结束时间" prop="examEndTime">
          <el-date-picker
            v-model="form.examEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="考试教室" prop="classRoomId">
          <el-select v-model="form.classRoomId" clearable filterable style="width:100%">
            <el-option
              v-for="item in classRoomOptions"
              :label="item.levelName"
              placeholder="选择考试教室"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="监考老师" prop="invigilationTeacher">
          <el-select
            v-model="form.invigilationTeacher"
            filterable
            placeholder="请选择"
            style="width:100%"
          >
            <el-option
              v-for="item1 in userOptions"
              :key="item1.id"
              :label="item1.username"
              :value="item1.username"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="学期">
          <el-select v-model="form.termId" placeholder="请选择" style="width:100%">
            <el-option
              v-for="item1 in termTimeList"
              :key="item1.id"
              :label="item1.tetiBeginYear+'~'+item1.tetiEndYear"
              :value="item1.id"
            >
              <span style="float: left">{{ item1.tetiBeginYear+'~'+item1.tetiEndYear }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item1.remarks }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio v-model="form.status" label="1">启用</el-radio>
          <el-radio v-model="form.status" label="0">停用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--考试管理模板导出-->
    <el-dialog title="考试录入模板导出" :visible.sync="exportOpen" width="30%">
      <el-form ref="templateForm" :model="templateForm" :rules="rulesTemplate" label-width="120px">
        <el-form-item label="考试名称" prop="examName">
          <el-input v-model="templateForm.examName" placeholder="请输入班级名称" />
        </el-form-item>
        <!-- <el-form-item label="考试专业" prop="classSubject">
            <el-select v-model="form.classSubject" clearable filterable style="width:100%">
              <el-option v-for="item in subjectList" :label="item.subjectName" placeholder="班级专业" :value="item.id" :key="item.id"></el-option>
            </el-select>
        </el-form-item>-->
        <el-form-item label="考试课程" prop="curriculumTypes">
          <el-select v-model="templateForm.curriculumTypes" multiple clearable filterable style="width:100%">
            <el-option
              v-for="item in courseTypeOptions"
              :label="item.dictLabel"
              placeholder="选择考试课程"
              :value="item.dictValue"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试班级" prop="classId">
          <el-select v-model="templateForm.classId" clearable filterable style="width:100%">
            <el-option
              v-for="item in classOptions"
              :label="item.className"
              placeholder="选择考试班级"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" icon="el-icon-download" @click="exoprtTemp">导出模板</el-button>
      </div>
    </el-dialog>

    <!--考试管理模板导出-->
    <el-dialog title="考试录入模板导出" :visible.sync="importOpen" width="30%">
      <el-form ref="importForm" :model="importForm" :rules="rulesImport" label-width="120px">
        <el-form-item label="考试开始时间" prop="examBeginTime">
          <el-date-picker
            v-model="importForm.examBeginTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="考试结束时间" prop="examEndTime">
          <el-date-picker
            v-model="importForm.examEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="上传文件" >
          <el-upload
              class="upload-demo"
              ref="upload"
              :action="upload.url"
              :data="importForm"
              :headers="upload.headers"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              :auto-upload="false">
              <el-button slot="trigger" size="mini" type="primary">选择文件</el-button>
            </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" icon="el-icon-upload2" @click="importSubmit">提交考试信息</el-button>
      </div>
    </el-dialog>

  </div>
</template>
  
  <script>
import {
  findSchoolClassPageList,
  findTeacherClassPageList,
  addOrUpdateSchoolClassInfo,
  deleteSchoolClassInfo,
  exportBClass,
  batchUpdateClassStatus
} from "@/api/classManage/classInfo";
import {
  pageList,
  editExam,
  deleteExam,
  deleteBatchExam,
  updateBatchExam,
  exportTemplate
} from "@/api/studentInfo/examManage";
import { findBaseSpaceLocationPageList } from "@/api/baseData/siteManagement";
import { getClassTeacherList } from "@/api/system/user";
import { findBaseSubjectPageList } from "@/api/baseData/profession";
import { listTermtime } from "@/api/system/termtime.js";
import { getToken } from '@/utils/auth'

export default {
  name: "BClass",
  data() {
    return {
      upload:{
        url:process.env.VUE_APP_BASE_API + "/exam/info/import",
        headers: { Authorization: "Bearer " + getToken() },
      },
      importOpen:false,
      exportOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      rowList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      stuCount: 0,
      // 字典表格数据
      bclassList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 班主任
      userOptions: [],
      // 状态数据字典
      statusOptions: [],
      // 班级所属部门
      classDeptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: undefined,
        status: undefined,
        classDept: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" }
        ],
        examBeginTime: [
          { required: true, message: "考试开始时间不能为空", trigger: "blur" }
        ],
        examEndTime: [
          { required: true, message: "考试结束时间不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "考试班级不能为空", trigger: "blur" }
        ],
        classRoomId: [
          { required: true, message: "考试教室不能为空", trigger: "blur" }
        ],
        curriculumType: [
          { required: true, message: "考试课程不能为空", trigger: "blur" }
        ]
      },
      rulesTemplate:{
        examName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" }
        ],
        curriculumTypes: [
          { required: true, message: "考试课程不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "考试班级不能为空", trigger: "blur" }
        ],
      },
      rulesImport:{
          examBeginTime: [
            { required: true, message: "请选择考试开始时间", trigger: "blur" }
          ],
          examEndTime: [
            { required: true, message: "请选择考试结束时间", trigger: "blur" }
          ],
      },
      subjectList: [],
      termTimeList: [],
      courseTypeOptions: [],
      classOptions: [],
      classRoomOptions: [],
      templateForm:{},
      importForm:{},
      fileList:[]
    };
  },
  created() {
    this.getList();
    this.getClassTeacherList();
    this.getSubjectList();
    this.getTermTimeList();
    this.getClassList();
    this.getClassRoom();
    this.getDicts("t_sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("class_dept").then(response => {
      this.classDeptOptions = response.data;
    });
    this.getDicts("course_type").then(response => {
      this.courseTypeOptions = response.data;
    });
  },
  methods: {
    handlePreview(){

    },
    handleRemove(){

    },
    //提交上传
    importSubmit(){
      this.$refs["importForm"].validate(valid => {
        if (valid) {
          this.$refs.upload.submit();
        }
      })
    },
    //下载模板
    exoprtTemp(){
      this.$refs["templateForm"].validate(valid => {
        if (valid) {
          exportTemplate(this.templateForm).then(response=>{
            this.fileDownload(response.data, "考试录入模板.xlsx");
          })
        }
      })
      
    },
    handleImport(){
      this.resetForm("importForm");
      this.importOpen = true
    },
    //打开模板导出页面
    HandleImportTemp(){
      this.resetForm("templateForm");
      this.exportOpen = true
    },
    getClassRoom() {
      findBaseSpaceLocationPageList({
        pageNum: 1,
        pageSize: 1000,
        locationType: 4
      }).then(response => {
        this.classRoomOptions = response.data.list;
      });
    },
    getClassList() {
      //查询班级
      findTeacherClassPageList({
        pageSize: 500,
        pageNum: 1,
        status: 1,
        isGraduation: 0
      }).then(resp => {
        this.classOptions = resp.data.list;
      });
    },
    getTermTimeList() {
      //查询学期
      this.loading = true;
      listTermtime({ pageSize: 100, pageNum: 1, status: 1 }).then(response => {
        this.termTimeList = response.data.list;
      });
    },
    getSubjectList() {
      //查询专柜
      findBaseSubjectPageList({ pageSize: 100, pageNum: 1, status: 1 }).then(
        response => {
          this.subjectList = response.data.list;
        }
      );
    },
    /** 查询班级列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.bclassList = response.data.list;
        console.log(response.data.list);
        console.log('=====')
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getClassTeacherList() {
      getClassTeacherList().then(response => {
        this.userOptions = response.data;
      });
    },
    // 字典设备状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 字典设备状态字典翻译
    classDeptFormat(row, column) {
      return this.selectDictLabel(this.classDeptOptions, row.classDept);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        className: undefined,
        classTeacherId: undefined,
        status: "1",
        classDept: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级信息";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.rowList = selection;

      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.form = JSON.parse(JSON.stringify(row));
      // this.form.classSubject = row.classSubject?Number(row.classSubject):null
      this.form.semester = this.form.semester
        ? Number(this.form.semester)
        : null;
      this.form.classId = this.form.classId ? Number(this.form.classId) : null;
      this.form.termId = this.form.termId ? Number(this.form.termId) : null;
      this.form.classRoomId = this.form.classRoomId
        ? Number(this.form.classRoomId)
        : null;
      this.form.classTeacherId = Number(this.form.classTeacherId);
      this.form.assistantTeacharId = Number(this.form.assistantTeacharId);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          editExam(this.form).then(resp => {
            this.msgSuccess("提交成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return deleteExam(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },

    // 获取当前选中班主任的名称
    selectUser(val) {
      alert(val.label);
      this.form.username = val.label;
    },
    operationStatus(flag) {
      //修改状态
      let str = "启用";
      if (flag == 0) {
        str = "停用";
      }
      for (let i = 0; i < this.rowList.length; i++) {
        this.rowList[i].status = flag;
      }
      updateBatchExam(this.rowList).then(resp => {
        this.msgSuccess(str + "成功");
        this.getList();
      });
    },
    handleDeleteBatch() {
      //批量删除
      this.$confirm("是否确认删除选择的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteBatchExam(this.rowList).then(resp => {
            this.getList();
            this.msgSuccess("删除成功");
          });
        })
        .catch(function() {});
    }
  }
};
</script>
  